﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using LeShiTao_Library.WeChat;
using System.Drawing.Printing;
using System.Net;
using System.Text.RegularExpressions;
using System.Threading;
using Seagull.BarTender.Print;

namespace TB_WinForm.HongBao
{
    public partial class Express_HongBao : Form
    {
        public Express_HongBao()
        {
            InitializeComponent();
        }
        private static string JSON_TICKER_STR="";
        private static string Sceneid;
        private static string QR_URL;
        private static string Tids;
        private static string BTW_LOCAL = Application.StartupPath + @"\BarCode\" + "HB_QR.btw";
        private void Express_HongBao_Load(object sender, EventArgs e)
        {
            checkBox1.Checked = true;
            button1.Enabled = false;
            PrintAddList();//加载打印机
            textBox1.Focus();
            comboBox2.SelectedIndex = 0;
        }
        private void PrintAddList()
        {
            PrintDocument print = new PrintDocument();
            string sDefault = print.PrinterSettings.PrinterName;//默认打印机名
            foreach (string sPrint in PrinterSettings.InstalledPrinters)//获取所有打印机名称
            {
                comboBox2.Items.Add(sPrint);
            }
        }
        
        private void textBox1_KeyDown(object sender, KeyEventArgs e)
        {
            
            string HB_Type = "Wait_Collect";
            if (checkBox2.Checked==false)
            {
                HB_Type = "Wait_Collect";
            }
            else if (checkBox2.Checked == true)
            {
                HB_Type = "Wait_Collect_Rate_2";
            }
            else
            {
                HB_Type = "Wait_Collect";
            }
            if(e.KeyCode==Keys.Enter)
            {
                this.textBox1.Enabled = false;
                if (textBox1.Text == "11223344")
                {
                    
                    if (this.openFileDialog1.ShowDialog() == DialogResult.OK)
                    {
                        string FileName = this.openFileDialog1.FileName;
                        BTW_LOCAL = FileName;
                        MessageBox.Show("您选择的打印模板路径为：" + BTW_LOCAL);
                        // 你的 处理文件路径代码 
                    }
                }
                else
                {
                    if (checkBox3.Checked)
                    {
                        string Tid = SysInfo.WSTOP.GET_WL_ID_USE_TID(SysInfo.Seller_Nick,
                            textBox1.Text.Trim().ToString(), "WAIT_BUYER_CONFIRM_GOODS");
                        if(Tid=="")
                        { MessageBox.Show("该物流单号无法在系统中找到订单号，请检查是否同步物流以及订单号状态是否为已出库", "警告"); }
                        else
                        {
                            string HB_Insert_Info = SysInfo.WSHBL.INSERT_HB_SERVER(Convert.ToInt32(SysInfo.Seller_Info.Tables[0].Rows[0][0].ToString()),
                            SysInfo.WX_User.ToString(), SysInfo.Seller_Nick + " 仓库红包端", Tid,
                            Convert.ToDouble(comboBox1.Text.Trim().ToString()), HB_Type);
                            if (HB_Insert_Info.Split('@')[0] == "0")
                            {
                                //正常
                                string[] HB_Info_List = HB_Insert_Info.Split('@')[1].Split('#');
                                string QR_Scene_ID = HB_Info_List[0].ToString();
                                string QR_Ticket = HB_Info_List[1].ToString();
                                string QR_URL_S = HB_Info_List[2].ToString();
                                JSON_TICKER_STR = QR_Ticket;
                                Sceneid = QR_Scene_ID;
                                QR_URL = QR_URL_S;

                                pictureBox1.ImageLocation = @"https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket=" + QR_Ticket.ToString();
                                Tids = Tid.Trim().ToString();
                                textBox1.Enabled = true;
                                textBox1.Text = "";
                                textBox1.Focus();
                            }
                            else
                            {
                                MessageBox.Show("创建失败 失败原因：" + HB_Insert_Info.Split('@')[1].ToString());
                                this.textBox1.Enabled = true;
                            }

                        }
                    }
                    else
                    {
                        string HB_Insert_Info = SysInfo.WSHBL.INSERT_HB_SERVER(Convert.ToInt32(SysInfo.Seller_Info.Tables[0].Rows[0][0].ToString()),
                            SysInfo.WX_User.ToString(), SysInfo.Seller_Nick + " 仓库红包端", textBox1.Text.Trim().ToString(),
                            Convert.ToDouble(comboBox1.Text.Trim().ToString()), HB_Type);
                        if (HB_Insert_Info.Split('@')[0] == "0")
                        {
                            //正常
                            string[] HB_Info_List = HB_Insert_Info.Split('@')[1].Split('#');
                            string QR_Scene_ID = HB_Info_List[0].ToString();
                            string QR_Ticket = HB_Info_List[1].ToString();
                            string QR_URL_S = HB_Info_List[2].ToString();
                            JSON_TICKER_STR = QR_Ticket;
                            Sceneid = QR_Scene_ID;
                            QR_URL = QR_URL_S;

                            pictureBox1.ImageLocation = @"https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket=" + QR_Ticket.ToString();
                            Tids = textBox1.Text.Trim().ToString();
                            textBox1.Enabled = true;
                            textBox1.Text = "";
                            textBox1.Focus();
                        }
                        else
                        {
                            MessageBox.Show("创建失败 失败原因：" + HB_Insert_Info.Split('@')[1].ToString());
                            this.textBox1.Enabled = true;
                        }
                    }

                }


               
            }
        }

        private void pictureBox1_LoadCompleted(object sender, AsyncCompletedEventArgs e)
        {
           
                if (checkBox1.Checked)
                {
                    Thread FH_Print_Thread = new Thread(new ThreadStart(FH_Print));
                    FH_Print_Thread.Start();
                }
            
            
        }

        private void FH_Print()
        {
            //word打印方式
            //Document doc = new Document(@"Word_Temp\FH_Temp.docx");
            //doc.Range.Replace("$安全码$", Sceneid, false, false);
            //Regex reg = new Regex("&图片&");
            //doc.Range.Replace(reg, new ReplaceAndInsertImage(@"QR\" + JSON_TICKER_STR + ".png"), false);
            //doc.Print(comboBox2.Text.Trim().ToString());
            try {
                LabelFormatDocument labelFormat;
                using (Engine btEngine = new Engine(true))
                {
                    labelFormat = btEngine.Documents.Open(BTW_LOCAL);//载入模板
                    labelFormat.SubStrings["QR_URL"].Value = QR_URL;
                    labelFormat.SubStrings["SAFECODE"].Value = Sceneid;
                    string Buyer_Nick = SysInfo.WSTOP.Get_Buyer_Nick_Tids(Tids, SysInfo.Seller_Nick.ToString()).ToString();
                    labelFormat.SubStrings["BUYERNICK"].Value = Buyer_Nick;
                    labelFormat.PrintSetup.PrinterName = comboBox2.Text.Trim().ToString();
                    labelFormat.PrintSetup.IdenticalCopiesOfLabel = 1;//打印数量
                    labelFormat.Print(Sceneid + DateTime.Now.ToString(), 1);//开始打印
                }
            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.Message.ToString());
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Thread FH_Print_Thread = new Thread(new ThreadStart(FH_Print));
            FH_Print_Thread.Start();
        }

        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
            if(checkBox1.Checked==true)
            {
                button1.Enabled = false;
            }
            else if(checkBox1.Checked==false)
            {
                button1.Enabled = true;
            }
        }
    }
}
